﻿using MulCodeDiscern.Classes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
using ZXing;
using ZXing.QrCode.Internal;

namespace MulCodeDiscern
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        #region 全局变量
        string[] tabName = null;//选项卡的所有名称
        #region 窗体
        Size window_size = new Size(980, 500);//窗体大小
        #endregion

        #region 界面：批量生成
        string fileSrc = "";//文本来源路径|界面：批量生成
        List<string> fileContent = new List<string>();//存储读取的文本内容
        List<string> file_number = new List<string>();

        int tag_index = 0;
        string currentDir = Directory.GetCurrentDirectory();//当前软件的执行目录
        string currentDir_Image = "";//当前目录下的子目录
        #endregion


        #region 图片二维码
        string img_path = "";//图片本地路径
        #endregion
        #endregion

        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMakeCode_Click(object sender, EventArgs e)
        {
            if (txtCodeString.Text.Trim().Length > 0)
            {
                picCode.Image = GenByZXingNet(txtCodeString.Text.Trim());
                Log("生成成功");
            }
        }
        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="msg">二维码信息</param>
        /// <returns>图片</returns>
        private Bitmap GenByZXingNet(string msg,int pixels=600)
        {
            Bitmap img = null;
            try
            {

                BarcodeWriter writer = new BarcodeWriter();
                writer.Format = BarcodeFormat.QR_CODE;
                writer.Options.Hints.Add(EncodeHintType.CHARACTER_SET, cmbCHARACTER.SelectedItem.ToString());//编码问题
                writer.Options.Hints.Add(
                    EncodeHintType.ERROR_CORRECTION,
                    levels[cmbErrorCorrectionLevel.SelectedIndex]
                );
                 int codeSizeInPixels = pixels;   //设置图片长宽
                writer.Options.Height = writer.Options.Width = codeSizeInPixels;
                writer.Options.Margin = Convert.ToInt32(txtMargin.Text);//设置边框
                ZXing.Common.BitMatrix bm = writer.Encode(msg);
                img = writer.Write(bm);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return img;
        }

        private void btnUp_Click(object sender, EventArgs e)
        {

            OpenFileDialog ofd = new OpenFileDialog();   //创建一个对话框对象
            ofd.Title = "请选择上传的图片";  //为对话框设置标题
            ofd.Filter = "所有文件|*.*|图片格式|*.jpg|透明格式|*.png";  //设置筛选的图片格式
            ofd.Multiselect = false;        //设置是否允许多选

            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string filePath = ofd.FileName;//获得文件的完整路径（包括名字后后缀）
                //txtUploadFile.Text = filePath;//将文件路径显示在文本框中
                int position = filePath.LastIndexOf("\\");
                string fileName = filePath.Substring(position + 1);
                using (Stream stream = ofd.OpenFile())
                {
                    //using (FileStream fs = new FileStream(fileName, FileMode.Create))
                    //{
                    //    stream.CopyTo(fs);
                    //    fs.Flush();
                    //}

                    picCode.ImageLocation = fileName;
                }
                txtCodeString.Text = "";
                button1.Text = "点我识别";
                Log("选择成功，请点击 【点我识别】");
            }
            else
            {
                Log("取消选择");
            }
        }

        /// <summary>
        /// 解密二维码
        /// </summary>
        /// <param name="barcodeBitmap">图片</param>
        /// <returns>二维码信息</returns>
        private string DecodeQrCode(Bitmap barcodeBitmap)
        {
            BarcodeReader reader = new BarcodeReader();
            reader.Options.CharacterSet = cmbCHARACTER.SelectedItem.ToString();
            var result = reader.Decode(barcodeBitmap);
            return (result == null) ? null : result.Text;
        }
        /// <summary>
        /// 开启解密
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (picCode.Image != null)
            {
                txtCodeString.Text = DecodeQrCode(picCode.Image as Bitmap);
                Log("识别成功");
            }
            button1.Text = "识别";
        }

        private void txtCodeString_TextChanged(object sender, EventArgs e)
        {
            if (txtCodeString.Text.Trim().Length > 0)
            {
                btnMakeCode.Enabled = true;
            }
            else
            {
                btnMakeCode.Enabled = false;

            }
        }

        ErrorCorrectionLevel[] levels = null;
        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            //初始化窗口标题和版本
            this.Text = StaticFn.GetAppSettingValue("appName");// +
            //" _ " +
            //StaticFn.GetAppSettingValue("author") + "\t" +
            //StaticFn.GetAppSettingValue("version");
            //默认初始选项卡的布局大小
            tabControl1.Width = 520;
            this.Height = window_size.Height;
            //选项卡的名称
            int tabCount = tabControl1.TabPages.Count;//选项卡数量
            tabName = new string[tabCount];
            for (int i = 0; i < tabCount; i++)//获取每个选项卡的Text值给数组
            {
                tabName[i] = tabControl1.TabPages[i].Text;
            }

            //当前保存图像的目录|批量生成界面
            currentDir_Image = currentDir + "\\Image";


            //默认单个生成文本框内容
            txtCodeString.Text = "输入文字，点击生成";

            //加载设置
            levels = new ErrorCorrectionLevel[]{
                            ErrorCorrectionLevel.L,
                            ErrorCorrectionLevel.M,
                            ErrorCorrectionLevel.Q,
                            ErrorCorrectionLevel.H
                        };
            cmbErrorCorrectionLevel.Items.AddRange(levels);
            cmbErrorCorrectionLevel.SelectedIndex = levels.Length - 1;
            cmbCHARACTER.SelectedIndex = 0;
            picBar.Image = QRcodeSystem.Global.QrCode.GenerateBarCode("123456789012", picBar.Width, picBar.Height);

            //执行一次默认生成QrCode
            btnMakeCode_Click(null, null);
        }



        /// <summary>
        /// 保存二维码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSaveCode_Click(object sender, EventArgs e)
        {
            try
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.FileName = "二维码_" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second;
                sfd.Filter = "图片|*.bmp";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    Bitmap bmp = new Bitmap(picCode.Image);
                    bmp.Save(sfd.FileName,ImageFormat.Bmp);
                    Log("保存成功");
                }
            }
            catch (Exception ex)
            {
                tool.Text = ex.Message;
                // MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 日志提醒
        /// </summary>
        /// <param name="content"></param>
        private void Log(string content)
        {
            tool.Text = content;
        }
        string[] path = null;//存储二维码目录路径
        /// <summary>
        /// 选择二维码图片来源目录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFrom_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.SelectedPath = Environment.CurrentDirectory;
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtCodeFrom.Text = fbd.SelectedPath;
                Log("二维码来源文件夹选择成功，来源目录: " + fbd.SelectedPath);
                //得到某个文件夹下的文件名
                path = Directory.GetFiles(fbd.SelectedPath);
                Log("选择成功，总共有 " + path.Length + " 个文件");
            }
        }
        /// <summary>
        /// 文字保存路径选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnToText_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.SelectedPath = Environment.CurrentDirectory;
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtSaveText.Text = fbd.SelectedPath;
                Log("文本文件识别储存文件夹选择成功，将保存目录至: " + fbd.SelectedPath);
                btnStartDiscern.Enabled = true;
            }
        }
        /// <summary>
        /// 开始识别
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStartDiscern_Click(object sender, EventArgs e)
        {
            if (rPay.Checked)//支付版
            {
                timerPay.Enabled = true;
            }
            else
                timerDiscern.Enabled = true;
        }
        int count = 0;
        private void timerDiscern_Tick(object sender, EventArgs e)
        {
            if (path != null)
            {
                if (count < path.Length)
                {
                    picCode.Image = Image.FromFile(path[count]);
                    string temp = DecodeQrCode((Image.FromFile(path[count])) as Bitmap);
                    Log("第" + count + "个文件" + path[count] + "  识别为：" + temp);
                    string filename = Path.GetFileName(path[count]).Split('.')[0];
                    if (temp == null)
                    {
                        filename += " ------------ 无法识别";
                    }
                    SaveTextFile(temp, filename);
                    count++;
                }
                else
                {
                    Log((path.Length - 1) + "个二维码图片全部识别完成！");
                    timerDiscern.Enabled = false;
                }
            }
            else
            {
                timerDiscern.Enabled = false;
                Log("请选择二维码目录");
            }
        }
        /// <summary>
        /// 保存为文件
        /// </summary>
        /// <param name="text"></param>
        private void SaveTextFile(string text, string filename)
        {
            try
            {
                string savepath = txtSaveText.Text.Trim();
                if (savepath.Length > 0)//已经选择好保存目录
                {
                    using (StreamWriter sw = new StreamWriter(savepath + "\\" + filename + ".txt", false, Encoding.UTF8))
                    {
                        sw.Write(text);
                        Log(filename + ".txt 保存成功");
                    }
                }
            }
            catch (Exception ex)
            {
                Log(ex.Message);
            }

        }

        /// <summary>
        /// 选项卡切换设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = tabControl1.SelectedIndex;

            if (index == 2)//进入批量生成界面
            {
                tabControl1.Width = 912;//遮盖预览PictureBox控件
                //当前模块的使用说明
                lbUseTip.Text = "1.选择文本文件\r\n2.点击【一键生成】\r\n3.图片保存路径为当前软件目录下的【Image】目录";
            }
            else
            {
                tabControl1.Width = 520;
            }
            //提示
            Log("进入界面 :  " + tabName[index]);
        }
        /// <summary>
        /// 一键生成二维码|界面：批量生成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOneGenerate_Click(object sender, EventArgs e)
        {
            if (btnOneGenerate.Text == "停止")
            {
                if (MessageBox.Show("确定停止生成二维码吗？", "提示", MessageBoxButtons.YesNo) ==
                    System.Windows.Forms.DialogResult.Yes)
                {
                    StopGenerate();
                }
            }
            else
            {
                //路径识别
                if (fileSrc == "")
                {
                    MessageBox.Show("请选择文本来源文件.");
                    return;
                }
                //文本获取
                Log("正在读取文本数据");
                //952801006250001|http://
                string[] temp = File.ReadAllLines(fileSrc, Encoding.UTF8);
                if (temp.Length <= 0)
                {
                    MessageBox.Show("没有数据的文件");
                    return;
                }
                fileContent.AddRange(temp);
                Log("文本数据读取完成");

                //开始执行生成
                if (fileContent.Count > 0)//含有数据
                {
                    tag_index = 0;
                    timerBatchGenerate.Enabled = true;
                    btnOneGenerate.Text = "停止";
                    //处理操作在方法:【timerBatchGenerate_Tick】
                }
            }

        }

        /// <summary>
        /// 停止生成
        /// </summary>
        private void StopGenerate()
        {
            timerBatchGenerate.Enabled = false;
            //清除数据
            fileContent.Clear();
            file_number.Clear();
            btnOneGenerate.Text = "一键生成";
        }

        /// <summary>
        /// 文本来源选择|界面：批量生成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelectFile_Click(object sender, EventArgs e)
        {
            //选择一个文本文件
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "请选择一个文本文件";
            ofd.Filter = "文本文件|*.txt|所有文件|*.*";
            ofd.InitialDirectory = currentDir;
            DialogResult dr = ofd.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                ///判断格式*
                fileSrc = ofd.FileName;
                txtFileSrc.Text = fileSrc;
            }
        }

        /// <summary>
        /// 执行批量生成,若干毫秒执行一次|界面：批量生成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timerBatchGenerate_Tick(object sender, EventArgs e)
        {
            if (fileContent.Count > 0)//含有数据
            {
                string org_str = fileContent[0];

                //设置文件名称
                string filename = currentDir_Image + "\\" + (tag_index + 1) + "_" +
                    DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day +//年月日
                    DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second //时分秒
                    + ".png";
                //判断是否执行一次成功
                bool success = false;
                //参数配置选择
                if (rDefaultParams.Checked)//【默认配置】
                {
                    //生成并保存二维码图片|批量生成界面，默认配置选项
                    success = GenerateAndSave(filename);
                }


                if (rHighParams.Checked)//【高级配置】
                {
                    string row1 = txtParamRow1.Text;
                    string row2 = txtParamRow2.Text;
                    string row3 = txtParamRow3.Text;
                    string temp = "";
                    //文件名选项
                    if (cFilename.Checked)
                    {
                        int L = 0;
                        if (int.TryParse(row1, out L))
                        {
                            temp = SplitString(org_str, L);
                        }
                        else
                        {
                            StopGenerate();
                        }
                        //修改文件名为第一行数据|要保存唯一性
                        filename = currentDir_Image + "\\" + temp + "_" +
                                        DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day +//年月日
                                        DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second //时分秒
                                        + "_" + tag_index + ".png";
                    }
                    success = GenerateAndSave(filename, row1, row2, row3);
                }

                if (rThree.Checked)//【高配三】
                {
                    filename = currentDir_Image + "\\" + (tag_index + 1) + "_" +
                   DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day +//年月日
                   DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second //时分秒
                   + ".bmp";
                    success = GenerateAndSave(filename, 1);
                }
                if (success)
                    //列表框提示进度
                    richExecuteStatus.Text = "第\t" + ((tag_index++) + 1) + "  行生成成功!\n" +
                        richExecuteStatus.Text;
            }
            else//执行完成，进行提示
            {
                System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("Explorer.exe");
                psi.Arguments = "/e," + currentDir_Image;
                System.Diagnostics.Process.Start(psi);
                richExecuteStatus.Text = "生成完成\n" +
                   richExecuteStatus.Text;
                //timerBatchGenerate.Enabled = false;
                StopGenerate();
            }
        }

        #region 处理最终执行【默认配置】和【高级配置】,【高配三】的处理函数
        /// <summary>
        /// 【默认配置】生成并保存二维码图片|批量生成界面
        /// </summary>
        /// <param name="filename">包含后缀的文件名路径</param>
        public bool GenerateAndSave(string filename)
        {
            Bitmap bmp = new Bitmap(GenByZXingNet(fileContent[0]));
            fileContent.RemoveAt(0);
            bmp.Save(filename);
            return true;
        }

        /// <summary>
        /// 【高级配置】生成并保存二维码图片|批量生成界面
        /// </summary>
        /// <param name="filename">包含后缀的文件名路径</param>
        public bool GenerateAndSave(string filename, string row1, string row2, string row3)
        {
            string org_str = fileContent[0];
            Bitmap bmp = new Bitmap(GenByZXingNet(org_str,200));
            fileContent.RemoveAt(0);
            //新建一个画布
            Bitmap bitmap = new Bitmap(240, 320);
            int padding = 20;
            Graphics g = Graphics.FromImage(bitmap);
            g.FillRectangle(new SolidBrush(Color.White),
                          0, 0, bitmap.Width, bitmap.Height);
            g.DrawImage(bmp, new Point(padding, padding));
            //字体设置
            int font_size = 14;
            Font font = new System.Drawing.Font("微软雅黑", font_size, FontStyle.Regular);
            //添加第一行
            int L = 0;
            if (int.TryParse(row1, out L))
            {
                row1 = SplitString(org_str, L);
            }
            else
            {
                StopGenerate();
            }
            int row1_x = (bitmap.Width - (row1.Length) * (font_size - 3)) / 2;//第一行的起始x
            int row1_y = (bmp.Height + padding) + 5;//第一行的起始y  ，与上一行间距5px
            g.DrawString(row1, font, new SolidBrush(Color.Black), row1_x, row1_y);
            //添加第二行
            int row2_x = (bitmap.Width - (row2.Length) * (font_size + 6)) / 2;//第二行的起始x
            int row2_y = (bmp.Height + padding + font_size * 1 + 5 * 2) + 5;//第二行的起始y  ，与上一行间距5px
            g.DrawString(row2, font, new SolidBrush(Color.Black), row2_x, row2_y);
            //添加第三行
            int row3_x = (bitmap.Width - (row3.Length - 1) * font_size) / 2;//第三行的起始x
            int row3_y = (bmp.Height + padding + font_size * 2 + 5 * 4) + 5;//第三行的起始y  ，与上一行间距5px
            g.DrawString(row3, font, new SolidBrush(Color.Black), row3_x, row3_y);

            bitmap.Save(filename);
            return true;
        }

        /// <summary>
        /// 【高配三】|生成细节操作
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="number"></param>
        private bool GenerateAndSave(string filename, int flag)
        {
            string org_str = fileContent[0];
            if (!org_str.Contains("|"))
            {
                StopGenerate();
                MessageBox.Show("源文件内容格式不正确");
                return false;
            }
            //分离行
            string number = org_str.Substring(0, org_str.IndexOf('|'));
            string url = org_str.Substring(org_str.IndexOf('|') + 1);
            Bitmap bmp = new Bitmap(GenByZXingNet(url,200));
            fileContent.RemoveAt(0);
            //新建一个画布
            Bitmap bitmap = new Bitmap(210, 220);
            int padding = 5;
            Graphics g = Graphics.FromImage(bitmap);
            g.FillRectangle(new SolidBrush(Color.White),
                          0, 0, bitmap.Width, bitmap.Height);
            g.DrawImage(bmp, new Point(padding, padding));
            //字体设置
            int font_size = 14;
            Font font = new System.Drawing.Font("微软雅黑", font_size, FontStyle.Regular);
            //添加第一行

            int row1_x = (bitmap.Width - (number.Length) * (font_size - 3)) / 2;//第一行的起始x
            int row1_y = (bmp.Height) - 3;//第一行的起始y  ，与上一行间距1px
            g.DrawString(number, font, new SolidBrush(Color.Black), row1_x, row1_y);
            bitmap.Save(filename, System.Drawing.Imaging.ImageFormat.Bmp);
            return true;
        }

        /// <summary>
        /// 截取指定尾数长度的字符
        /// </summary>
        /// <param name="temp"></param>
        /// <returns></returns>
        public string SplitString(string orgString, int length)
        {
            return orgString.Substring(orgString.Length - length);
        }
        #endregion

        #region 默认|高级参数显隐
        /// <summary>
        /// 选中高级配置参数|批量生成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rHighParams_CheckedChanged(object sender, EventArgs e)
        {
            int extend_height = 200;
            if (window_size.Height >= this.Height)
            {
                this.Height += extend_height;
            }
        }

        /// <summary>
        /// 默认配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rDefaultParams_CheckedChanged(object sender, EventArgs e)
        {
            int extend_height = 200;
            if (window_size.Height < this.Height)
            {
                this.Height -= extend_height;

            }
        }
        #endregion

        #region 菜单处理函数
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void miniAbout_Click(object sender, EventArgs e)
        {

        }
        #endregion


        #region 支付版
        private void timerPay_Tick(object sender, EventArgs e)
        {
            if (path != null)
            {
                if (count < path.Length)
                {
                    Bitmap bmp = ClipBitmap(path[count], new Rectangle(750, 1310, 1000, 1000));
                    picCode.Image = bmp;
                    string temp = DecodeQrCode(bmp);
                    Log("第" + (count + 1) + "个文件" + path[count] + "  识别为：" + temp);
                    string filename = Path.GetFileName(path[count]).Split('.')[0];
                    //if (temp == null)
                    //{
                    //    filename = "_无法识别_"+filename;
                    //}
                    SaveTextFile(temp, (count + 1) + " _ " + filename);
                    count++;
                }
                else
                {
                    Log((path.Length - 1) + "个二维码图片全部识别完成！");
                    timerPay.Enabled = false;
                    count = 0;
                    //打开目录
                    System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("Explorer.exe");
                    psi.Arguments = "/e," + txtSaveText.Text.Trim();
                    System.Diagnostics.Process.Start(psi);
                }
            }
            else
            {
                timerPay.Enabled = false;
                count = 0;
                Log("请选择二维码目录");
            }
        }

        /// <summary>
        /// 截取区域
        /// </summary>
        /// <param name="filename">文件名</param>
        /// <param name="r">截取区域像素</param>
        /// <returns>截取到的图像</returns>
        public Bitmap ClipBitmap(string filename, Rectangle r)
        {
            Image img = Bitmap.FromFile(filename) as Image;
            Bitmap bmp = img as Bitmap;
            Bitmap bitmap = new Bitmap(r.Width + 1, r.Height + 1);
            for (int x = r.X; x <= (r.Width + r.X); x++)
            {
                for (int y = r.Y; y <= (r.Y + r.Height); y++)
                {
                    //取当前像素点
                    Color pixeColor = bmp.GetPixel(x, y);
                    byte R = pixeColor.R;
                    byte G = pixeColor.G;
                    byte B = pixeColor.B;
                    bitmap.SetPixel(x - r.X, y - r.Y, Color.FromArgb(R, G, B));
                    ////给新的值
                    //if (IsPixel(bmp, x, y))//黑色
                    //{
                    //    bitmap.SetPixel(x - r.X, y - r.Y, Color.Black);
                    //}
                }
            }
            return bitmap;
        }

        /// <summary>
        /// 判断当前位置是否为需要的颜色
        /// </summary>
        /// <param name="bmp"></param>
        /// <returns></returns>
        public bool IsPixel(Bitmap bmp, int x, int y)
        {
            Color pixeColor = bmp.GetPixel(x, y);
            byte R = pixeColor.R;
            byte G = pixeColor.G;
            byte B = pixeColor.B;
            int r = Convert.ToInt32(R);
            if (Convert.ToInt32(G) == 170)
            {
                return true;
            }
            if (r == 0)//黑色
            {
                return true;
            }
            return false;
        }
        #endregion

        /// <summary>
        /// 二维码名片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCardQr_Click(object sender, EventArgs e)
        {
            try
            {
                string path = Directory.GetCurrentDirectory();//hb.bmp
                path += "\\64bit_cardqrcode.exe";
                //path += "\\名片二维码生成器.exe";

                Base.RunProcess(path);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 关于软件|菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 关于软件AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form about = new About();
            about.ShowDialog();
        }


        /// <summary>
        /// 点击上传|图片二维码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUploader_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = Directory.GetCurrentDirectory();
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    System.Net.WebClient myWebClient = new System.Net.WebClient();
                    byte[] buffer = myWebClient.UploadFile(StaticFn.GetAppSettingValue("img_server") + "?act=upload", "POST", ofd.FileName);
                    string ret = Encoding.UTF8.GetString(buffer);

                    if (ret.Split('-')[0] == "ok")
                    {
                        //ret.Split('-')[1]的返回地址后缀
                        picCode.Image = GenByZXingNet(StaticFn.GetAppSettingValue("img_query_server") + "?no=" + ret.Split('-')[1]);
                        MessageBox.Show("上传成功");
                    }
                    else
                    {
                        MessageBox.Show("上传失败");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("上传失败,原因:" + ex.Message);
                }
            }
        }
        /// <summary>
        /// PDF417码生成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPDF417_Click(object sender, EventArgs e)
        {
            try
            {
                string path = Directory.GetCurrentDirectory();//hb.bmp
                path += "\\TestPDF417.exe";
                Base.RunProcess(path);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 生成条形码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBarQr_Click(object sender, EventArgs e)
        {
            string qrContent = txtCodeString.Text;
            picBar.Image = QRcodeSystem.Global.QrCode.GenerateBarCode(qrContent, picBar.Width, picBar.Height);
        }
        /// <summary>
        /// 保存条形码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSaveBar_Click(object sender, EventArgs e)
        {
            try
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.FileName = "条形码_" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second;
                sfd.Filter = "图片|*.bmp";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    Bitmap bmp = new Bitmap(picBar.Image);
                    bmp.Save(sfd.FileName,ImageFormat.Bmp);
                    Log("保存成功");
                }
            }
            catch (Exception ex)
            {
                tool.Text = ex.Message;
                // MessageBox.Show(ex.Message);
            }
        }

    }
}
